<?php

namespace app\common\model;

use think\Model;

class Address extends Model
{
    // 表名
    protected $name = 'address';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    
    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
	    'default_text',
	    'delete_text',
    ];
    

    const DEF_Y = 1;
    const DEF_D = 0;
    
    public static $defaultList = [
        self::DEF_Y => '是',
	    self::DEF_D => '否'
    ];
    
    public function getDefaultTextAttr($value, $data) {
    	$value = $value?$value:$data['is_default'];
    	$list = self::$defaultList;
    	return isset($list[$value])?$list[$value]:'';
    }

	const DEL_Y = 1;
	const DEL_N = 0;
	public static $deleteList = [
		self::DEF_Y => '是',
		self::DEF_D => '否'
	];
	public function getDeleteTextAttr($value, $data) {
		$value = $value?$value:$data['status'];
		$list = self::$deleteList;
		return isset($list[$value])?$list[$value]:'';
	}

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function province(){
    	return $this->belongsTo('Areas', 'province_id', 'id', '', 'left')->setEagerlyType(0);
    }
    
    public function city(){
    	return $this->belongsTo('Areas', 'city_id', 'id','', 'left')->setEagerlyType(0);
    }
    
    public function area(){
    	return $this->belongsTo('Areas', 'area_id', 'id','', 'left' )->setEagerlyType(0);
    }

    public function user(){
    	return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
    }
}
